package com.aiti.lulian.components.wx;

import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.SendCardExchangeDto;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/wx")
public class WxInfoController {

    @Value("${wx.app-id}")
    private String appId;

    @Value("${wx.app-secret}")
    private String appSecret;

    private RestTemplate restTemplate;

    private WxComponent wxComponent;

    private ILuLianEnterpriseService luLianEnterprise;

    private final ILuLianEnterpriseUserService enterpriseUserService;

    private final String GET_WX_USER_OPENID = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

    public WxInfoController(RestTemplate restTemplate, WxComponent wxComponent, ILuLianEnterpriseService luLianEnterprise,ILuLianEnterpriseUserService enterpriseUserService) {
        this.restTemplate = restTemplate;
        this.wxComponent = wxComponent;
        this.luLianEnterprise = luLianEnterprise;
        this.enterpriseUserService = enterpriseUserService;
    }

    @PostMapping("/getWxUserOpenId")
    public Message getWxUserOpenId(@RequestBody Map<String, Object> params){
        String code = (String) params.get("code");
        if(StringUtils.isBlank(code)){
            return Message.fail("没有找到Code");
        }
        String findUrl = String.format(GET_WX_USER_OPENID, "wx7ca766313703d868", "ee06b7594ea4239ab55153d1bae9461c", code);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(findUrl, String.class);
        if(forEntity.getStatusCode().is2xxSuccessful()){
            String body = forEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(body);
            String openId = jsonObject.getObject("unionid",String.class);
            return Message.success(openId);
        }else{
            return Message.fail("OpenId获取失败");
        }
    }

    @GetMapping("/{openId}")
    public Message sendCard(@PathVariable("openId") String openId){
//        boolean b = wxComponent.sendCardExchangeRequest(openId);
        return Message.success();
    }

    @PostMapping("/sendCardBiz")
    public Message sendCardBiz(@RequestBody SendCardExchangeDto dto){
        String enterpriseFinalName = dto.getEnterpriseName();
        if(StringUtils.isNotBlank(dto.getCompanyId())){
            LuLianEnterprise byId = luLianEnterprise.getById(dto.getCompanyId());
            if(byId != null && StringUtils.isNotBlank(byId.getBaseName())){
                enterpriseFinalName = byId.getBaseName();
            }
        }

        if(StringUtils.isBlank(enterpriseFinalName)) {
            LuLianEnterpriseUser enterpriseUser = enterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", dto.getFromUserId()));
            if (enterpriseUser != null && StringUtils.isNotBlank(enterpriseUser.getEnterpriseId())) {
                String enterpriseId = enterpriseUser.getEnterpriseId();
                LuLianEnterprise byId1 = luLianEnterprise.getById(enterpriseId);
                if(byId1 != null && StringUtils.isNotBlank(byId1.getBaseId())){
                    enterpriseFinalName = byId1.getBaseName();
                }
            }
        }
        dto.setEnterpriseName(enterpriseFinalName);
        boolean b = wxComponent.sendCardExchangeRequest(dto);
        return Message.success(b);
    }

}
